package com.demo.dao.impl;

import com.demo.dao.LeaveDao;
import com.demo.entity.Leaveapplication;
import com.demo.util.Util;

import java.io.Serializable;
import java.sql.*;
import java.util.Date;
import java.util.*;

public class LeaveDaoImpl implements LeaveDao {

    public void add(Leaveapplication vo) {
        //id时自增的不用加
        String sql = "insert into `leaveapplication` (`reason`,`days`,`courseId`,`studentId`,`status`,`applitime`,`opinion`) values(?,?,?,?,?,?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);
            // TODO: 2023/11/13
//            ps.setLong(1, vo.getId());
            ps.setString(1, vo.getReason());
            ps.setLong(2, vo.getDays());
            ps.setLong(3, vo.getCourseId());
            ps.setLong(4, vo.getStudentId());
            ps.setString(5, vo.getStatus());
            ps.setString(6, vo.getApplitime());
            ps.setString(7, "");
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override

    public void update(Leaveapplication vo) {
        //你可以直接复制右边这个表
        //三个问号要传递三个值
        String sql = "update `leaveapplication` set `reason` = ? ,`days` = ? ,`courseId` = ?,`studentId` = ? ,`status`= ?,`applitime`= ?,`opinion`= ?,`audittime`= ? where `id` = ?";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);
            //这不就是三个值吗
            // TODO: 2023/11/11 先 get 再 set
            ps.setString(1, vo.getReason());
            ps.setLong(2, vo.getDays());
            ps.setLong(3, vo.getCourseId());
            ps.setLong(4, vo.getStudentId());
            ps.setString(5, vo.getStatus());
            ps.setString(6, vo.getApplitime());
            ps.setString(7, "");
            ps.setString(8, vo.getAudittime());
            ps.setLong(9, vo.getId());

            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public boolean delete(long id) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `leaveapplication` where id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //@Override
    public Leaveapplication get(Serializable id) {
        // TODO: 2023/11/11
        Leaveapplication vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `leaveapplication` where id = " + id;
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()) {
                vo = new Leaveapplication();
                // TODO: 2023/11/11 自己打个标记要改哪里
                vo.setId(rs.getLong("id"));
                vo.setReason(rs.getString("reason"));
                vo.setDays(rs.getLong("days"));
                vo.setCourseId(rs.getLong("courseId"));
                vo.setStudentId(rs.getLong("studentId"));
                vo.setStatus(rs.getString("status"));
                vo.setApplitime(rs.getString("applitime"));
                vo.setOpinion(rs.getString("opinion"));
                vo.setAudittime(rs.getString("audittime"));

            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    //@Override
    public Map<String, Object> list(Map<String, Object> params) {
        // TODO: 2023/11/11
        List<Leaveapplication> list = new ArrayList();

        int totalCount = 0;
        String condition = "";
        String sqlList;
        if (params.get("searchColumn") != null && !"".equals(params.get("searchColumn"))) {
            condition += " and `" + params.get("searchColumn") + "` like '%" + params.get("keyword") + "%'";
        }
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps;
            ResultSet rs;
            String limit = (params.get("startIndex") != null && params.get("pageSize") != null) ? " limit " + params.get("startIndex") + "," + params.get("pageSize") : "";
            sqlList = "select * from `leaveapplication` where 1=1 " + condition + " order by id asc " + limit + ";";
            ps = c.prepareStatement(sqlList);
            rs = ps.executeQuery();
            while (rs.next()) {

                // TODO: 2023/11/11
                Leaveapplication vo = new Leaveapplication();

                vo.setId(rs.getLong("id"));
                vo.setReason(rs.getString("reason"));
                vo.setDays(rs.getLong("days"));
                vo.setCourseId(rs.getLong("courseId"));
                vo.setStudentId(rs.getLong("studentId"));
                vo.setStatus(rs.getString("status"));
                vo.setApplitime(rs.getString("applitime"));
                vo.setOpinion(rs.getString("opinion"));
                vo.setAudittime(rs.getString("audittime"));
                list.add(vo);
            }
            // TODO: 2023/11/11  没有//
            String sqlCount = "select count(*) from `leaveapplication` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }

    @Override
    public boolean refuse(long id) {
        // 获取当前时间
        Date currentTime = new Date();
        // 转换为Timestamp类型
        Timestamp currentTimestamp = new Timestamp(currentTime.getTime());
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "update `leaveapplication` set `opinion`= '拒绝', `audittime` = '" + currentTimestamp + "' WHERE id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean agree(long id) {
        // 获取当前时间
        Date currentTime = new Date();
        // 转换为Timestamp类型
        Timestamp currentTimestamp = new Timestamp(currentTime.getTime());
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "UPDATE `leaveapplication` SET `opinion` = '同意', `audittime` = '" + currentTimestamp + "' WHERE id = " + id;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
